#include "showwidget.h"
#include "ui_showwidget.h"
#include <QTextStream>
#include <QDateTime>
#include <QSqlError>
#include <QDebug>
#include <QSqlRelationalDelegate>
#include <QStyledItemDelegate> // 标准委托适合无外键
#include <QSqlQueryModel>
#include <QSqlDatabase>
#include <QTimer>
#include <QStandardItemModel>
#include <QSqlRecord>
#include <QMessageBox>
ShowWidget::ShowWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("串口管理");

    this->readFile();

    this->initPortConfig(); // 端口配置
    this->initConfigModel(); // 模式配置


}

ShowWidget::~ShowWidget()
{
    delete ui;
}
// 写文件
void ShowWidget::writeFile(QString str)
{
    // 写文件
    QFile file(this->filePath);

    QString timenow = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    file.open(QIODevice::Append | QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << "[" << timenow << "] " << str << "\n";
//    file.write(str.toUtf8());
    file.close();
}

void ShowWidget::writeFile(QString str, QString str1)
{
    QFile file(this->filePath);
    QString timenow = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    file.open(QIODevice::Append | QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << "[" << timenow << QString::fromUtf8("] 用户:") << str + ":" << str1 << "\n";
    file.close();
}
// 读文件
void ShowWidget::readFile()
{
    QFile file(this->filePath);
    file.open(QIODevice::ReadOnly);
    QByteArray buf = file.readAll();
    ui->textEdit->setText(buf);
//    ui->textEdit->setText("你好");
    file.close();
}
// 配置模式
void ShowWidget::initConfigModel()
{
    this->ck2 = new QSqlTableModel(this);

    this->ck2->setTable("configinfo");
    ui->tableView_2->setModel(this->ck2);
    this->ck2->select();
    ui->tableView_2->setItemDelegate(new QItemDelegate(ui->tableView_2));
    // 修改表头
    this->ck2->setHeaderData(0,Qt::Horizontal,"端口号");
    this->ck2->setHeaderData(1,Qt::Horizontal,"CR解释");
    this->ck2->setHeaderData(2,Qt::Horizontal,"LF解释");
    this->ck2->setHeaderData(3,Qt::Horizontal,"连接数");
    this->ck2->setHeaderData(4,Qt::Horizontal,"对端主机");
    this->ck2->setHeaderData(5,Qt::Horizontal,"对端端口");
    this->ck2->setHeaderData(6,Qt::Horizontal,"连接命令");
    this->ck2->setHeaderData(7,Qt::Horizontal,"断开命令");
    this->ck2->setHeaderData(8,Qt::Horizontal,"空闲时间");

    // 设置表格展示方式 根据大小自动调整宽度
    ui->tableView_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
    ui->tableView_2->resizeColumnsToContents(); // 根据内容自动调整一次
    ui->tableView_2->setAlternatingRowColors(true);

    this->ck2->setEditStrategy(QSqlTableModel::OnManualSubmit); // 设置手动提交
}
// 端口模式
void ShowWidget::initPortConfig()
{
    this->ck = new QSqlTableModel(this);

    this->ck->setTable("portinfo");
    ui->tableView->setModel(this->ck);
    this->ck->select();
    ui->tableView->setItemDelegate(new QItemDelegate(ui->tableView));
    // 修改表头
    this->ck->setHeaderData(0,Qt::Horizontal,"端口号");
    this->ck->setHeaderData(1,Qt::Horizontal,"串口号");
    this->ck->setHeaderData(2,Qt::Horizontal,"串口名");
    this->ck->setHeaderData(3,Qt::Horizontal,"波特率");
    this->ck->setHeaderData(4,Qt::Horizontal,"数据位");
    this->ck->setHeaderData(5,Qt::Horizontal,"停止位");
    this->ck->setHeaderData(6,Qt::Horizontal,"校验位");
    this->ck->setHeaderData(7,Qt::Horizontal,"控制位");
    this->ck->setHeaderData(8,Qt::Horizontal,"接收空闲时间");
    this->ck->setHeaderData(9,Qt::Horizontal,"发送空闲时间");
    // 设置表格展示方式 根据大小自动调整宽度
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
    ui->tableView->resizeColumnsToContents(); // 根据内容自动调整一次
    ui->tableView->setAlternatingRowColors(true);

    this->ck->setEditStrategy(QSqlTableModel::OnManualSubmit); // 设置手动提交
}

void ShowWidget::on_textEdit_textChanged()
{
    this->ischanged = true;
}

void ShowWidget::on_pushButton_clicked()
{
    if (this->ischanged == true)
    {
        this->readFile();
    }
}

void ShowWidget::on_pushButton_5_clicked()
{
    // 刷新视图
    this->initPortConfig();
}
// 端口模式提交
void ShowWidget::saveChanges()
{
    this->ck->submitAll();
}
// 端口模式添加
void ShowWidget::addData()
{
    this->initPortConfig();
    QSqlRecord record = this->ck->record();
    qDebug() << "用户点击添加！";
    int count = this->ck->rowCount();
    qDebug() << count;
    // 将空记录插入到模型中
    this->ck->insertRecord(count,record);
}
// 配置模式提交
void ShowWidget::on_pushButton_6_clicked()
{
    this->ck2->submitAll();
}
// 端口模式提交
void ShowWidget::on_pushButton_4_clicked()
{
    this->ck->submitAll();

}

void ShowWidget::on_pushButton_2_clicked()
{
    this->addData();
}
// 端口模式删除
void ShowWidget::on_pushButton_3_clicked()
{
    // 获取所有选中的行对象
    QItemSelectionModel* selectionModel = ui->tableView->selectionModel();

    // 从选中的对象中得到行号 得到的是一个链表
    // 链表中每一个单元都是一个qmodelindex对象
    QModelIndexList list = selectionModel->selectedRows();

    QMessageBox box1(
                QMessageBox::Warning,
                "是否确认删除",
                QString("你确定要删除 %1 条数据吗？").arg(list.size()),
                QMessageBox::Yes |
                QMessageBox::Cancel,
                this
                );

    box1.setButtonText(QMessageBox::Yes,QString("删除%1条数据").arg(list.size()));
    box1.setButtonText(QMessageBox::No,"取消");
    int result = box1.exec();
    if (result == QMessageBox::Yes)
    {
        for (QModelIndex ele : list)
        {
             // 遍历链表得到具体的行号，在进行删除
    //        qDebug() << ele.row();
            this->ck->removeRow(ele.row());
        }

        this->ck->submitAll();
    }
}
// 配置模式添加
void ShowWidget::on_pushButton_8_clicked()
{
    this->initConfigModel();
    QSqlRecord record = this->ck2->record();
    qDebug() << "用户点击添加！";
    int count = this->ck2->rowCount();
    qDebug() << count;
    // 将空记录插入到模型中
    this->ck2->insertRecord(count,record);
}
// 配置模式删除
void ShowWidget::on_pushButton_7_clicked()
{
    // 获取所有选中的行对象
    QItemSelectionModel* selectionModel = ui->tableView_2->selectionModel();

    // 从选中的对象中得到行号 得到的是一个链表
    // 链表中每一个单元都是一个qmodelindex对象
    QModelIndexList list = selectionModel->selectedRows();

    QMessageBox box1(
                QMessageBox::Warning,
                "是否确认删除",
                QString("你确定要删除 %1 条数据吗？").arg(list.size()),
                QMessageBox::Yes |
                QMessageBox::Cancel,
                this
                );

    box1.setButtonText(QMessageBox::Yes,QString("删除%1条数据").arg(list.size()));
    box1.setButtonText(QMessageBox::No,"取消");
    int result = box1.exec();
    if (result == QMessageBox::Yes)
    {
        for (QModelIndex ele : list)
        {
             // 遍历链表得到具体的行号，在进行删除
    //        qDebug() << ele.row();
            this->ck2->removeRow(ele.row());
        }

        this->ck2->submitAll();
    }
}
// 模式配置刷新
void ShowWidget::on_pushButton_9_clicked()
{
    this->initConfigModel();
}
